package com.topteam.graph;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:Create by 2021/6/22
 * Describe: 折线图
 * author: leo
 * Email: le112233@126.com
 */
public class Fragment_LineChart extends Fragment {

    private View view;
    private CombinedChart line_v;//折线图
    private CombinedData lineData1;  //折线图数据1

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.line_fragment, container, false);
        line_v = view.findViewById(R.id.line);
        showLineChart();
        return view;

    }

    /**
     * 展现折线图
     */
    private void showLineChart() {
        //设置图表描述
        Legend legend = line_v.getLegend();
        legend.setTextColor(Color.WHITE);
        legend.setTextSize(15);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        Description description = new Description();
        description.setXOffset(200);
        description.setYOffset(-50);
        description.setTextSize(20);
        description.setTextColor(Color.YELLOW);
        description.setText("生产不良表");
        line_v.setDescription(description);
        //设置X轴数据
        setAxisXBottom1();
        //设置Y轴右边数据格式
        setAxisYRight1();
        //设置Y轴左边数据格式
        setAxisYLeft1();
        //设置折线图数据
        lineData1 = new CombinedData();
        lineData1.setData(getLineData1());
        line_v.setData(lineData1);
        line_v.setTouchEnabled(false);
        line_v.getDescription().setEnabled(false);
        line_v.setDrawGridBackground(false);
        line_v.setDrawBarShadow(false);
        line_v.setHighlightFullBarEnabled(false);
        line_v.animateX(2000);

    }

    /**
     * 设置横坐标数据1
     */
    private void setAxisXBottom1() {
        List<String> titles = new ArrayList<>();
        titles.add("起泡");
        titles.add("烧伤");
        titles.add("变形");
        titles.add("异色");
        titles.add("色差");
        titles.add("穿孔");
        titles.add("折弯");
        titles.add("多孔");
        titles.add("白点");
        titles.add("原料不良");
        titles.add("报废");
        titles.add("其他");

        //X轴的数据格式
        XAxis xAxis = line_v.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(titles));
        //设置位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置是否绘制网格线
        xAxis.setDrawGridLines(false);
        line_v.getAxisLeft().setDrawGridLines(false);
        // barChart.animateY(2500);
        //设置X轴文字剧中对齐
        xAxis.setCenterAxisLabels(false);
        //X轴最小间距
        xAxis.setGranularity(1f);
        //设置柱子的总数量
        xAxis.setLabelCount(12);
        //设置字体大小
        xAxis.setTextSize(10);
        //设置第一条柱子距离左边的距离
        xAxis.setSpaceMin(0.4f);
        xAxis.setSpaceMax(0.4f);
        xAxis.setTextColor(Color.WHITE);
    }

    /**
     * 设置右侧纵坐标数据1
     */
    private void setAxisYRight1() {
        YAxis right = line_v.getAxisRight();
        right.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return value + "%";
            }
        });
        right.setDrawGridLines(false);
        right.setAxisMinimum(0f);//为坐标轴设置最小值
        right.setAxisMaximum(100);
        right.setTextSize(12);
        right.setTextColor(Color.parseColor("#00FFFF"));
        right.setEnabled(false);
    }

    /**
     * 设置左侧纵坐标数据1
     */
    private void setAxisYLeft1() {
        YAxis left = line_v.getAxisLeft();
        left.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int aa = (int) value;
                return aa + "";
            }
        });
        //是否绘制网格线
        left.setDrawGridLines(false);
        left.setAxisMinimum(0f);//为坐标轴设置最小值
        left.setAxisMaximum(100);

        left.setDrawZeroLine(false);
        left.setTextSize(12);
        left.setTextColor(Color.parseColor("#00FF00"));
    }

    /**
     * 设置折线图绘制数据1
     *
     * @return
     */
    public LineData getLineData1() {

        List<Entry> entries1 = new ArrayList<>();
        entries1.add(new Entry(0,88));
        entries1.add(new Entry(1,34));
        entries1.add(new Entry(2,66));
        entries1.add(new Entry(3,87));
        entries1.add(new Entry(4,56));
        entries1.add(new Entry(5,10));
        entries1.add(new Entry(6,90));
        entries1.add(new Entry(7,22));
        entries1.add(new Entry(8,45));
        entries1.add(new Entry(9,90));
        entries1.add(new Entry(10,65));
        entries1.add(new Entry(11,97));

        //设置描述
        LineDataSet lineDataSet1 = new LineDataSet(entries1,  "2019");
        //设置折线图依据哪边取值
        lineDataSet1.setAxisDependency(YAxis.AxisDependency.LEFT);
        //设置折线的颜色
        lineDataSet1.setColor(Color.parseColor("#00FFFF"));
        lineDataSet1.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return value + "";
            }
        });
        //设置圆点大小
        lineDataSet1.setCircleRadius(4);
        //设置折线大小
        lineDataSet1.setLineWidth(1);
        //设置字体大小
        lineDataSet1.setValueTextSize(11);
        //设置字体颜色
        lineDataSet1.setValueTextColor(Color.parseColor("#00FFFF"));

        /**
         * 第二条折线图
         */
        List<Entry> entries2 = new ArrayList<>();
        entries2.clear();
        entries2.add(new Entry(0,30));
        entries2.add(new Entry(1,20));
        entries2.add(new Entry(2,40));
        entries2.add(new Entry(3,50));
        entries2.add(new Entry(4,20));
        entries2.add(new Entry(5,70));
        entries2.add(new Entry(6,85));
        entries2.add(new Entry(7,33));
        entries2.add(new Entry(8,11));
        entries2.add(new Entry(9,34));
        entries2.add(new Entry(10,90));
        entries2.add(new Entry(11,43));

        //设置描述
        LineDataSet lineDataSet2 = new LineDataSet(entries2, "2020");
        //设置折线图依据哪边取值
        lineDataSet2.setAxisDependency(YAxis.AxisDependency.LEFT);
        //设置折线的颜色
        lineDataSet2.setColor(Color.parseColor("#00FF00"));
        lineDataSet2.setValueFormatter(new IValueFormatter() {

            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return value + "";
            }
        });
        //设置圆点大小
        lineDataSet2.setCircleRadius(4);
        //设置折线大小
        lineDataSet2.setLineWidth(1);
        //设置字体大小
        lineDataSet2.setValueTextSize(11);
        //设置字体颜色
        lineDataSet2.setValueTextColor(Color.parseColor("#00FF00"));

        LineData lineData = new LineData();
        lineData.addDataSet(lineDataSet1);
        lineData.addDataSet(lineDataSet2);

        return lineData;
    }
}
